﻿using DragonSolution.Web.Common.Common;
using DragonSolution.Web.Data.Entity;
using DragonSolution.Web.Data.Enum;
using DragonSolution.Web.UI.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DragonSolution.Web.UI.Controllers
{
    public class AccountController : LoginBaseController
    {
        private MyDbContext db;
        public AccountController() {
            db = base.GetContext();
        }

        [HttpGet]
        public ActionResult Login() {
            return View();
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult PostLogin() {
            string result;
            try
            {
                string account = Request.Form["account"];
                string pwd = Request.Form["pwd"];
                //md5加密
                 string password = StringUtil.MD5Encrypt(pwd);
                //验证
                var userInfo = db.UserInfo.FirstOrDefault(x => x.UserName == account && x.UserPwd == password);
                if (userInfo != null)
                {
                    var user = new UserInfo();
                    user.UserID = userInfo.UserID;
                    user.UserName = userInfo.UserName;
                    user.UserCategory = userInfo.UserCategory;
                    user.CreateTime = userInfo.CreateTime;
                    //写入Cookie
                    base.SetCurrentUser(user);
                    result = ConstDim.operateSuccess;
                }
                else
                {
                    result = ConstDim.operateFailed;
                }
                return Json(new { OperateResult = result });
            }
            catch (Exception exp)
            {
                return Json(new { OperateResult = ConstDim.operateFailed, ResponseData = exp.Message });
            }
        }

        /// <summary>
        /// 退出登录
        /// </summary>
        /// <returns></returns>
        public ActionResult logOff()
        {            
            //删除Cookie
            Response.Cookies[ConstDim.CookieKeyUser].Expires = DateTime.Now.AddDays(-1);
            return Redirect(ConstDim.IndexPath);
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult PostRegister()
        {
            try
            {
                string account = Request.Form["account"];
                string pwd = Request.Form["pwd"];
                //md5加密
                string password = StringUtil.MD5Encrypt(pwd);
                //插入
                UserInfo userInfo = new UserInfo();
                userInfo.UserName = account;
                userInfo.UserPwd = password;
                userInfo.UserCategory = Convert.ToInt32(UserCategory.管理员);
                userInfo.CreateTime = DateTime.Now;
                db.UserInfo.Add(userInfo);
                db.SaveChanges();

                return Json(new { OperateResult = ConstDim.operateSuccess });
            }
            catch (Exception exp)
            {
                return Json(new { OperateResult = ConstDim.operateFailed, ResponseData = exp.Message });
            }
        }
    }
}